import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ClickGame extends JFrame implements ActionListener {
    private JButton btnClick2;
    private JButton btnClick1;
    private JProgressBar pgbClicks;
    private JLabel lblMessage;
    private JComboBox cboDifficulty;
    private JButton btnStartGame;
    private JPanel frmClickGame;
    private JLabel lblSelectDifficulty;
    private JProgressBar pgbTimer;
    private Timer timer;

    public static void main(String[] args) {
        ClickGame clickGame = new ClickGame();

        JFrame frame = new JFrame("Jordan's Click Game");
        frame.setContentPane(clickGame.frmClickGame);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(200, 200);
        frame.setVisible(true);
        frame.setMaximumSize(frame.getSize());

        clickGame.btnClick1.addActionListener(clickGame);
        clickGame.btnClick2.addActionListener(clickGame);
        clickGame.btnStartGame.addActionListener(clickGame);
    }

    public void changePgbProgress(String whichBar, Integer integer) {
        if (whichBar.equals("Clicks")) {
            pgbClicks.setValue(integer);
            pgbClicks.setString((integer / pgbClicks.getMaximum()) + "%");
        } else if (whichBar.equals("Timer")) {
            pgbTimer.setString(integer + "secs.");
            pgbTimer.setValue(integer);
        }
    }

    public void click(Integer whichButton) {
        if (whichButton == 1) {
            btnClick1.setEnabled(false);
            btnClick2.setEnabled(true);
        } else if (whichButton == 2) {
            btnClick1.setEnabled(true);
            btnClick2.setEnabled(false);
        }

        changePgbProgress("Clicks", pgbClicks.getValue() + 1);

        if (pgbClicks.getValue() >= pgbClicks.getMaximum())
            endGame("W");
    }

    public void setDifficulty() {
        if (cboDifficulty.getSelectedItem() == "Easy (10 Clicks)")
            pgbClicks.setMaximum(10);
        else if (cboDifficulty.getSelectedItem() == "Medium (20 Clicks")
            pgbClicks.setMaximum(20);
        else if (cboDifficulty.getSelectedItem() == "Hard (30 Clicks)")
            pgbClicks.setMaximum(30);
        else if (cboDifficulty.getSelectedItem() == "Insane (50 Clicks)") {
            pgbClicks.setMaximum(50);
            changePgbProgress("Timer", 15);
            pgbTimer.setMaximum(15);
        }

    }

    public void startGame() {
        setDifficulty();

        lblSelectDifficulty.setVisible(false);
        cboDifficulty.setVisible(false);
        btnStartGame.setVisible(false);

        pgbClicks.setVisible(true);
        pgbTimer.setVisible(true);
        btnClick1.setVisible(true);
        btnClick2.setVisible(true);

        timer = new Timer(1000, this);
        timer.start();
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == btnClick1)
            click(1);
        else if (e.getSource() == btnClick2)
            click(2);
        else if (e.getSource() == btnStartGame)
            startGame();
        else if (e.getSource() == timer) {
            changePgbProgress("Timer", pgbTimer.getValue() - 1);
            if (pgbTimer.getValue() == 0 && pgbClicks.getValue() < pgbClicks.getMaximum())
                endGame("L");
        }
    }

    public void endGame(String result) {
        if (result.equals("W"))
            lblMessage.setText("Congrats! You won :)");
        else if (result.equals("L"))
            lblMessage.setText("Sorry! You lost :(");
        lblMessage.setVisible(true);
        btnClick1.setEnabled(false);
        btnClick2.setEnabled(false);
        timer.stop();
    }

}
